package com.zhijiang.hospital.controller;

import com.alibaba.excel.EasyExcel;
import com.zhijiang.hospital.entity.dto.AisOperRegDto;
import com.zhijiang.hospital.service.AisOperRegDtoService;
import com.zhijiang.util.BaseQuery;
import com.zhijiang.util.PageList;
import com.zhijiang.util.Result;
import com.zhijiang.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author 75936
 * @version 1.0
 * @project zhijiang_hospital_data
 * @description
 * @date 2023-02-13 16:26:28
 */
@Slf4j
@Api(value = "手术麻醉数据", tags = "手术麻醉数据")
@RestController
@RequestMapping("AisOperReg")
public class AisOperRegDtoController {

    @Autowired
    AisOperRegDtoService aisOperRegDtoService;

    @ApiOperation("手术麻醉数据")
    @ApiModelProperty(value = "手术麻醉数据")
    @GetMapping("findAisOperRegDto")
    public Result findAisOperRegDto(String startTime, String endTime) {
        List<AisOperRegDto> all = aisOperRegDtoService.findAisOperRegDto(startTime, endTime);
        return Result.ok(all);
    }

    @ApiOperation("规定时间的手术麻醉数据分页")
    @ApiModelProperty(value = "手术麻醉数据分页")
    @PostMapping("findAisOperRegDtoBaseQuery")
    public Result findAisOperRegDtoBaseQuery(@RequestBody BaseQuery baseQuery) {
        Map<String, Object> aisOperRegDtos = aisOperRegDtoService.findAisOperRegDtos(TimeUtil.initBaseQuery(baseQuery));
        PageList<AisOperRegDto> aisOperRegDtosByPageList = aisOperRegDtoService.findAisOperRegDtosByPageList(TimeUtil.initBaseQuery(baseQuery));
        aisOperRegDtos.put("jihe", aisOperRegDtosByPageList);
        return Result.ok(aisOperRegDtos);
    }

    @ApiOperation("导出指定的住院绩效考核excel表格")
    @ApiModelProperty(value = "导出指定的住院绩效考核excel表格")
    @GetMapping("loadExcel")
    public void loadExcel(HttpServletResponse response, String startTime, String endTime) throws IOException {
        List<AisOperRegDto> aisOperRegDto = aisOperRegDtoService.findAisOperRegDto(startTime, endTime);
        log.error("导出指定的住院绩效考核数据", aisOperRegDto);
        String title = "导出指定的住院绩效考核数据";
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("手术麻醉运行数据.xlsx", "utf-8"));
        //4. 创建输入、输出流
        EasyExcel.write(response.getOutputStream(), AisOperRegDto.class).sheet(title).doWrite(aisOperRegDto);
    }

    @ApiOperation("手术麻醉数据分页对比数据，今年")
    @GetMapping("findYearData")
    public Result findYearData() {
        List<AisOperRegDto> yearData = aisOperRegDtoService.findYearData();
        return Result.ok(yearData);
    }

    @ApiOperation("手术麻醉数据分页对比数据，去年")
    @GetMapping("findLastYearData")
    public Result findLastYearData() {
        List<AisOperRegDto> lastYearData = aisOperRegDtoService.findLastYearData();
        return Result.ok(lastYearData);
    }

    @ApiOperation("手术麻醉数据折线图")
    @GetMapping("findBrokenLine")
    public Result findBrokenLine(String startTime, String endTime) {
        return Result.ok(aisOperRegDtoService.getAisOperRegData(startTime, endTime));
    }
}
